function doFormSubmission(elSubmitButton, strControlClientID) { var bErrorActivated = false; var strMandatoryErrorMessage = ''; var regexMandatory = new RegExp(" ", "g"); var regexEmail = new RegExp(strValidEmailRegex.replace(/&/g, '&'), "g"); function ActivateError(strElName, strErrorText, SPANerrorText) { if (SPANerrorText) { SPANerrorText.style.display = "block"; SPANerrorText.innerHTML = 'This ' + strErrorText; } else { strMandatoryErrorMessage = strMandatoryErrorMessage + '"' + strElName + '" ' + strErrorText + '
'; } bErrorActivated = true; } var strClientID, strContainerID; if (strControlClientID) { strClientID = strControlClientID; strContainerID = strClientID + '_elFormContents'; } else { if (!elSubmitButton) elSubmitButton = document.getElementById('ArticleSubmitButton'); strContainerID = $e(elSubmitButton).parents('[id$="_elFormContents"]').attr('id'); strClientID = strContainerID.replace('_elFormContents', ''); } var strArticleID = document.getElementById(strClientID + '_FormDocumentID').value; var form; var $form = $e('#' + strContainerID + ' form.form-for-submission'); if ($form.length) $form.empty(); else $form = $e('
'); form = $form.get(0); var blnCustomReplyTo = false; var blnCustomSubject = false; var arrRadioGroupsScanned = []; $e('#' + strContainerID + ' input, #' + strContainerID + ' textarea, #' + strContainerID + ' select').each(function () { var elm = $e(this); if (elm.prop('tagName') != 'SELECT') { var id = elm.attr('id'); if (id) { if (id.indexOf('_FormDocumentID') > 0) return true; //next iteration } var strType = elm.attr('type') ? elm.attr('type') : 'text'; strType = strType.toLowerCase(); switch (strType) { case 'button': case 'file': case 'image': case 'submit': return true; case 'text': //Remove any current error messages var SPANerrorText = document.getElementById(elm.id + '_errortext'); if (SPANerrorText) { SPANerrorText.style.display = "none"; SPANerrorText.innerHTML = ''; } if (elm.val().replace(regexMandatory, '') == '') { //If it's blank, check if it's mandatory var strMandatoryAttr = elm.attr('lang'); if (strMandatoryAttr && (strMandatoryAttr != '')) { ActivateError(elm.attr('name'), 'is a required field', SPANerrorText); } } else { //Else check for enforced field types. var strAccept = elm.attr('accept') ? elm.attr('accept') : ''; strAccept = strAccept.toLowerCase(); switch (strAccept) { case 'whole_number': if (!elm.val().match(/^\d+$/)) ActivateError(elm.attr('name'), 'must be a positive whole number', SPANerrorText); break; case 'number': if (!elm.val().match(/^\-?(\d+\.?|\.)\d*$/)) ActivateError(elm.attr('name'), 'must be a number', SPANerrorText); break; case 'email': if (!elm.val().match(regexEmail)) { ActivateError(elm.attr('name'), 'must be an email address', SPANerrorText); } break; case 'email_reply': if (!elm.val().match(regexEmail)) { ActivateError(elm.attr('name'), 'must be an email address', SPANerrorText); break; } if (!blnCustomReplyTo) { var elmEmail = document.createElement('input'); elmEmail.type = 'hidden'; elmEmail.name = 'NotificationReplyTo'; elmEmail.value = elm.val(); form.appendChild(elmEmail); blnCustomReplyTo = true; } break; case 'subject': if (!blnCustomSubject) { var elmSubject = document.createElement('input'); elmSubject.type = 'hidden'; elmSubject.name = 'NotificationSubject'; elmSubject.value = elm.val(); form.appendChild(elmSubject); blnCustomSubject = true; } break; } } break; case 'checkbox': //Remove any current error messages var SPANerrorText = document.getElementById(elm.id + '_errortext'); if (SPANerrorText) { SPANerrorText.style.display = "none"; SPANerrorText.innerHTML = ''; } if (!elm[0].checked) { //If it's blank, check if it's mandatory var strMandatoryAttr = elm.attr('lang'); if (strMandatoryAttr && (strMandatoryAttr != '')) { ActivateError(elm.attr('name'), 'is a required field', SPANerrorText); } } break; case 'radio': //Remove any current error messages var SPANerrorText = document.getElementById(elm.id + '_errortext'); if (SPANerrorText) { SPANerrorText.style.display = "none"; SPANerrorText.innerHTML = ''; } //Check radio group as a whole then record that we've checked it var strRadioGroup = elm.attr('name'); if (!arrRadioGroupsScanned.includes(strRadioGroup)) { var $allRadiosInGroup = $("#" + strContainerID + " input[name='" + strRadioGroup + "']"); if ($allRadiosInGroup.filter(":checked").length == 0 && $allRadiosInGroup.filter("[lang='1']").length > 0) { ActivateError(strRadioGroup, 'is a required field', SPANerrorText); } arrRadioGroupsScanned.push(strRadioGroup); } break; } } var elSubmit = elm.clone(); elSubmit.val(elm.val()); //Textareas, selects lose value when cloning form.appendChild(elSubmit.get(0)); }); if (strMandatoryErrorMessage != '') { ShowOverlay(strMandatoryErrorMessage + '
Please correct your form.' + '
'); return false; } if (bErrorActivated) { return false; } else { document.getElementById(strContainerID).appendChild(form); if (typeof Recaptcha !== 'undefined' && typeof Recaptcha.Lookup[strClientID] !== 'undefined') Recaptcha.Execute(strClientID); else SubmitValidForm(strClientID); } } function ShowOverlay(strHTML) { var divOverlay = $e('
'); divOverlay.html(strHTML); divOverlay.dialog({ modal: true, resizable: false, minHeight: 50, dialogClass: 'shadedbox', create: function (event, ui) { $e(this).closest('.ui-dialog').wrap('
'); }, open: function (event, ui) { $e('.ui-widget-overlay').wrap('
'); var dz = $e('.ui-front').css('z-index'); $e('.ui-widget-overlay').css({ 'z-index': dz - 1 }); $e('.ui-dialog-titlebar').css({ 'display': 'none' }); $e(this).dialog('option', 'width', '300'); $e(this).parent().css({ 'position': 'fixed' }); $e(this).parent().position({ my: 'center', at: 'center', of: '.ui-widget-overlay:visible' }); }, close: function (event, ui) { $e('.endis-jquery-ui').filter(function () { if ($e(this).text() == "") { return true; } return false; }).remove(); } }); divOverlay.parent().css({ 'position': 'fixed' }); divOverlay.parent().position({ my: 'center', at: 'center', of: '.ui-widget-overlay:visible' }); return divOverlay; } function SubmitValidForm(strClientID) { var strArticleID = document.getElementById(strClientID + '_FormDocumentID').value; var strRecaptchaResponse = (typeof Recaptcha !== 'undefined') ? Recaptcha.GetResponse(strClientID) : ''; var strContainerID = strClientID + '_elFormContents'; var form = $e('#' + strContainerID + ' form.form-for-submission').get(0); if (intMobileGroupID) { form.action = '/Publisher/SubmitForm.aspx?recaptcha_response=' + strRecaptchaResponse + '&article_id=' + strArticleID + '&mobile_group_id=' + intMobileGroupID; form.method = 'POST'; form.style.display = 'none'; form.submit(); } else { var divOverlay = ShowOverlay(' Submitting....'); var SubmitFailed = function () { divOverlay.html('Submission failed
'); divOverlay.parent().css({ 'position': 'fixed' }); divOverlay.parent().position({ my: 'center', at: 'center', of: '.ui-widget-overlay:visible' }); }; $e.ajax({ type: 'POST', url: '/Publisher/SubmitForm.aspx?recaptcha_response=' + strRecaptchaResponse + '&article_id=' + strArticleID, data: $e(form).serialize(), success: function (data) { try { var objData = $e.parseJSON(data); if (objData.valid == 'true') { var strOnClick; if (qs.get("WA") == "true" || IsHubbPage()) { strOnClick = (typeof dlg == 'undefined' ? 'window.top.AppNavigate();' : 'InsightAreYouSure.SuspendForPostback(); dlg.Close();'); } else { strOnClick = 'document.location.href =\'' + objData.destination + '\''; } divOverlay.html(objData.message + '
' + '
'); divOverlay.dialog('option', 'width', 300); divOverlay.dialog('option', 'height', 'auto'); divOverlay.parent().css({ 'position': 'fixed' }); divOverlay.parent().position({ my: 'center', at: 'center', of: '.ui-widget-overlay:visible' }); } else { SubmitFailed(); } } catch (e) { SubmitFailed(); } }, error: function (data) { SubmitFailed(); } }); } }